#include "../include/statusServer_client_rpc.h"
#include "../include/conf_reader.h"
#include "../include/user_list_manger.h"

statusServer_client_rpc::statusServer_client_rpc(){
    std::string ip_port=conf_reader::get_conf_reader_instance()->get_value_from_key("statusServer_address","192.168.174.139:10000"); 
    _channel=grpc::CreateChannel(ip_port,grpc::InsecureChannelCredentials());
    _stub=message::statusServer::NewStub(_channel);

    //开启一个游离线程去发送心跳包
    this->heart_beat_thread=std::thread([this](){
        while(1){   
            std::this_thread::sleep_for(std::chrono::seconds(HEART_BEAT_GAP));
            this->heart_beat();
        }
    });

    this->heart_beat_thread.detach();
}

bool statusServer_client_rpc::register_chat_server()
{
    message::reqRegisterChat req;
    grpc::ClientContext context;
    message::resRegisterChat  rps;
    req.set_host(conf_reader::get_conf_reader_instance()->get_value_from_key("local_host","192.168.174.139"));
    req.set_port(conf_reader::get_conf_reader_instance()->get_value_from_key("local_bind_port","12345"));
    req.set_rpc_server_info(user_list_manger::get_instance()->rpc_service_address);
    grpc::Status sts=_stub->registerChat(&context,req,&rps);
    if(!sts.ok()){
        return false;
    }
    if(rps.result()==true){
        return true;
    }else{
        return false;
    }
    return false;
}

bool statusServer_client_rpc::check_token(std::string &email, std::string &token)
{
    grpc::ClientContext context;
    message::reqCheckToken req;
    message::resCheckToken res;
    req.set_email(email);
    req.set_token(token);
    grpc::Status sts=_stub->checkToken(&context,req,&res);
    if(!sts.ok()){
        std::cout<<"check_token error"<<sts.error_code()<<": "<<sts.error_message()<<std::endl;
        return false;
    }
    return res.result();
}

bool statusServer_client_rpc::delete_chat_server()
{
    grpc::ClientContext context;
    message::reqDeleteChat req;
    req.set_host(conf_reader::get_conf_reader_instance()->get_value_from_key("local_host","192.168.174.139"));
    req.set_port(conf_reader::get_conf_reader_instance()->get_value_from_key("local_bind_port","12345"));
    message::resDeleteChat res;
    grpc::Status sts=_stub->deleteChat(&context,req,&res);
    if(sts.ok()){
        std::cout<<"成功删除注册的服务"<<std::endl;
    }else{
        std::cout<<"删除注册的服务失败"<<std::endl;
    }
    
    return false;
}

bool statusServer_client_rpc::heart_beat()
{
    grpc::ClientContext context;
    message::reqHeartBeat req;
    message::resHeartBeat res;
    req.set_host(conf_reader::get_conf_reader_instance()->get_value_from_key("local_host","192.168.174.139"));
    req.set_port(conf_reader::get_conf_reader_instance()->get_value_from_key("local_bind_port","12345"));
    req.set_rpc_server_info(user_list_manger::get_instance()->rpc_service_address);
   
    grpc::Status sts=_stub->heartBeat(&context,req,&res);
    if(sts.ok()){
        if(res.nothing()==true){
            std::cout<<"成功发送心跳"<<std::endl;
        }else{
            std::cout<<"发送成功，但是服务端没找着，怎么回事！！！！"<<std::endl;
        }
        
    }else{
        std::cout<<"发送心跳失败"<<std::endl;
    }
    return false;   //这里返回什么无所谓
}


std::string statusServer_client_rpc::get_forward_target(const std::string &email){
    grpc::ClientContext context;
    message::reqGetForwardTarget req;
    message::resGetForwardTarget res;
    req.set_email(email);
    grpc::Status sts=_stub->getForwardTarget(&context,req,&res);
    std::string tar_ip_port;
    if(sts.ok()){
        if(res.result()==true){
            tar_ip_port=res.ip_port();
            std::cout<<"获得的其他chat_server的rpc地址为："<<tar_ip_port<<std::endl;
        }
    }
    
    return tar_ip_port;
}


